package com.ben.multiple.shard.readwritespring;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ben.multiple.shard.readwritespring.mp.Teacher;
import com.ben.multiple.shard.readwritespring.mp.TeacherMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/rw")
public class RwController {
    @Resource
    private TeacherMapper teacherMapper;

    @GetMapping("/insert")
    public Map<String, Object> insert() {
        List<Teacher> list = new ArrayList<>();
        list.add(new Teacher(null, "马超", 18));
        list.add(new Teacher(null, "张良", 28));
        list.add(new Teacher(null, "王昭君", 16));
        list.add(new Teacher(null, "西施", 22));
        for (Teacher obj : list) {
            teacherMapper.insert(obj);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        return map;
    }

    @GetMapping("/delete")
    public Map<String, Object> delete(@RequestParam(value = "id") Long id) {
        Teacher teacher = teacherMapper.selectById(id);
        if (teacher != null) {
            teacherMapper.deleteById(id);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("teacher", teacher);
        return map;
    }


    @GetMapping("/select")
    public Map<String, Object> select() {
        List<Teacher> teachers = teacherMapper.selectList(Wrappers.emptyWrapper());

        Map<String, Object> map = new HashMap<>();
        map.put("teachers", teachers);
        return map;
    }

}
